package com.itheima.mobilesafe69.activity;

import com.itheima.mobilesafe69.R;
import com.itheima.mobilesafe69.utils.ConstantValue;
import com.itheima.mobilesafe69.utils.MD5Utils;
import com.itheima.mobilesafe69.utils.PrefUtils;
import com.itheima.mobilesafe69.utils.ToastUtil;
import com.startapp.android.publish.StartAppAd;
import com.startapp.android.publish.StartAppSDK;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

public class HomeActivity extends Activity {
	private GridView gv_home;
	private int[] mDrawableIds;
	private String[] mDes;
	private StartAppAd startAppAd = new StartAppAd(this);

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		StartAppSDK.init(this, "207830307", true);
		
		setContentView(R.layout.activity_home);
		
		initUI();
		initData();
	}
	
	@Override
	public void onResume() {
	    super.onResume();
	    startAppAd.onResume();
	}
	
	@Override
	public void onPause() {
	    super.onPause();
	    startAppAd.onPause();
	}
	
	@Override
	public void onBackPressed() {
	    startAppAd.onBackPressed();
	    super.onBackPressed();
	}

	private void initData() {
		//gv_home在内部需要去填充数据,形成多个方格
		//1,拿到GridView对象
		//2,给数据适配器去准备数据,集合,数组去做填充
		//3,把数据填充到GridView数据适配器中,用作展示
		//初始化图片数组
		mDrawableIds = new int[]{
				R.drawable.home_safe,R.drawable.home_callmsgsafe,R.drawable.home_apps,
				R.drawable.home_taskmanager,R.drawable.home_netmanager,R.drawable.home_trojan,
				R.drawable.home_sysoptimize,R.drawable.home_tools,R.drawable.home_settings
		};
		mDes = new String[]{
				"手机防盗","通信卫士","软件管理","进程管理","流量统计","手机杀毒","缓存清理","高级工具","设置中心"
		};
		gv_home.setAdapter(new MyAdapter());
		//设置一个条目的点击事件
		gv_home.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
				//点中条目的索引值
				switch (position) {
				case 0:
					showDialog();
					break;
				case 1:
					startActivity(new Intent(HomeActivity.this, BlackNumberActivity.class));
					break;
				case 2:
					startActivity(new Intent(HomeActivity.this, AppManagerActivity.class));
					break;
				case 3:
					startActivity(new Intent(HomeActivity.this, ProcessManagerActivity.class));
					break;
				case 4:
					startActivity(new Intent(HomeActivity.this,TrafficActivity.class));
					break;
				case 5:
					startActivity(new Intent(HomeActivity.this, AntiVirusActivity.class));
					break;
				case 6:
					startActivity(new Intent(HomeActivity.this, BaseClearCacheActivity.class));
					break;
				case 7:
					startActivity(new Intent(HomeActivity.this, AToolActivity.class));
					break;
				case 8:
					//跳转到设置界面
					startActivity(new Intent(HomeActivity.this, SettingActivity.class));
					break;
				}
			}
		});
		
	}

	protected void showDialog() {
		String psd = PrefUtils.getString(this,ConstantValue.SET_PSD, "");
		if(TextUtils.isEmpty(psd)){
			//1,初次进入设置密码对话框
			showSetPsdDialog();
		}else{
			//2,再进入应用确认密码对话
			showConfirmPsdDialog();
		}
	}
	private void initUI() {
		gv_home = (GridView) findViewById(R.id.gv_home);
	}
	
	class MyAdapter extends BaseAdapter{
		//返回listview中数据条目个数,gridview返回条目(方格)个数
		@Override
		public int getCount() {
			return mDes.length;
		}

		//返回position指向当前条目使用对象
		@Override
		public Object getItem(int position) {
			return null;
		}

		//返回条目索引position
		@Override
		public long getItemId(int position) {
			return position;
		}

		//返回一个条目对应的view,给其填充上数据
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			//1,构建一个条目相应布局文件
			View view = View.inflate(getApplicationContext(),R.layout.gridview_item_home, null);
			
			ImageView iv_icon = (ImageView) view.findViewById(R.id.iv_icon);
			iv_icon.setBackgroundResource(mDrawableIds[position]);
			
			
			TextView tv_des = (TextView) view.findViewById(R.id.tv_des);
			tv_des.setText(mDes[position]);
			
			//返回view,代表此view需要去做展示
			return view;
		}
	}
	
	/**
	 * 确认密码对话框
	 */
	private void showConfirmPsdDialog() {
		//对话框和activity绑定,所以必须传递activity对象
		Builder builder = new AlertDialog.Builder(this);
		//获取对话框对象
		final AlertDialog dialog = builder.create();
		//修改对话框的样式(布局结构)
		View view = View.inflate(this,R.layout.dialog_confirm_psd, null);
//		dialog.setView(view);
		dialog.setView(view, 0, 0, 0, 0);
		
		//找到对话框中所有控件
		Button bt_submit = (Button) view.findViewById(R.id.bt_submit);
		Button bt_cancel = (Button) view.findViewById(R.id.bt_cancel);
		
		final EditText et_confirm_psd = (EditText) view.findViewById(R.id.et_confirm_psd);
		
		bt_submit.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				//如果用户没有输入两次密码,告知用户输入密码	
				String confirmPsd = et_confirm_psd.getText().toString().trim();
				String spPsd = PrefUtils.getString(getApplicationContext(), ConstantValue.SET_PSD, "");
				
				if(!TextUtils.isEmpty(confirmPsd)){
					if(spPsd.equals(MD5Utils.encoder(confirmPsd))){
						//当前的对话框隐藏
						dialog.dismiss();
						//跳转到新的activity界面
						startActivity(new Intent(getApplicationContext(),MobileSafeActivity.class));
					}else{
						ToastUtil.show(getApplicationContext(), "输入密码错误");
					}
				}else{
					ToastUtil.show(getApplicationContext(), "输入密码");
				}
			}
		});
		
		bt_cancel.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				dialog.dismiss();
			}
		});
		
		//展示对话框
		dialog.show();
	}

	/**
	 * 自定义一个设置密码对话框
	 */
	private void showSetPsdDialog() {
		//对话框和activity绑定,所以必须传递activity对象
		Builder builder = new AlertDialog.Builder(this);
		//获取对话框对象
		final AlertDialog dialog = builder.create();
		//修改对话框的样式(布局结构)
		View view = View.inflate(this,R.layout.dialog_set_psd, null);
		
		//因为在2.3.3版本上,系统默认设置内间距,所以需要去除此内间距
//		dialog.setView(view);
		dialog.setView(view, 0, 0, 0, 0);
		
		//找到对话框中所有控件
		Button bt_submit = (Button) view.findViewById(R.id.bt_submit);
		Button bt_cancel = (Button) view.findViewById(R.id.bt_cancel);
		
		final EditText et_set_psd = (EditText) view.findViewById(R.id.et_set_psd);
		final EditText et_confirm_psd = (EditText) view.findViewById(R.id.et_confirm_psd);
		
		bt_submit.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				//如果用户没有输入两次密码,告知用户输入密码	
				String psd = et_set_psd.getText().toString().trim();
				String confirmPsd = et_confirm_psd.getText().toString().trim();
				if(!TextUtils.isEmpty(psd) && !TextUtils.isEmpty(confirmPsd)){
					if(psd.equals(confirmPsd)){
						//将确认正确的密码存储
						PrefUtils.putString(getApplicationContext(),ConstantValue.SET_PSD,MD5Utils.encoder(psd));
						//当前的对话框隐藏
						dialog.dismiss();
						
						//跳转到新的activity界面
						startActivity(new Intent(getApplicationContext(),MobileSafeActivity.class));
					}else{
						ToastUtil.show(getApplicationContext(), "两次密码不一致,重新输入");
					}
				}else{
					ToastUtil.show(getApplicationContext(), "输入密码");
				}
			}
		});
		
		bt_cancel.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				dialog.dismiss();
			}
		});
		
		//展示对话框
		dialog.show();
	}
}
